package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.QuickChangeConfigBean;
import com.koron.css2.baseConfig.mapper.QuickChangeConfigMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserFieldBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.dto.UserFieldConfigSelectBean;
import com.koron.css2.serviceManage.mapper.UserFieldConfigMapper;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

public class QuickChangeUserInfoQuery implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
        try {
            UserFieldBean bean = JsonUtils.objectToPojo(req.getData(), UserFieldBean.class);
            bean.setGroupCode(userInfo.getCurWaterCode());
            //获取mapper
            UserFieldConfigMapper mapper = factory.getMapper(UserFieldConfigMapper.class);
            //先根据页面获取运维平台分组
            List<String> groups=mapper.userFieldGroupSelect(bean);
            List<UserFieldConfigSelectBean> list=new ArrayList<UserFieldConfigSelectBean>();

            //再获取快速变更配置中的字段信息
            QuickChangeConfigMapper quickChangeConfigMapper = factory.getMapper(QuickChangeConfigMapper.class);
            List<QuickChangeConfigBean>  ListAccount= quickChangeConfigMapper.selectCodeNameByType("userInfoData");
            List<QuickChangeConfigBean>  ListInvoiceInfo= quickChangeConfigMapper.selectCodeNameByType("userWaterInfoData");


            List<String> userInfoData = new ArrayList<>();
            List<String> userWaterInfoData = new ArrayList<>();
            for (QuickChangeConfigBean configVO :ListAccount) {
                userInfoData.add(configVO.getCode());
            }
            for (QuickChangeConfigBean configVO :ListInvoiceInfo) {
                userWaterInfoData.add(configVO.getCode());
            }

            //运维平台中取出的字段与快速变更配置中一样的，才展示
            for(String group:groups) {
                bean.setGroup(group);
                //根据分组信息获取各个组的字段
                List<UserFieldBean> fields=mapper.userFieldConfigSelect(bean);
                List<UserFieldBean> fieldBeans = new ArrayList<>();
                if ("用户信息".equals(group)){
                    for (UserFieldBean userFieldBean : fields) {
                        userInfoData.forEach(infos ->{
                            if (infos.equals(userFieldBean.getFieldCode())){
                                fieldBeans.add(userFieldBean);
                            }
                        });
                    }
                } else if ("用户-水表信息".equals(group)){
                    for (UserFieldBean userFieldBean : fields) {
                        userWaterInfoData.forEach(infos -> {
                            if (infos.equals(userFieldBean.getFieldCode())) {
                                fieldBeans.add(userFieldBean);
                            }
                        });
                    }
                }
                UserFieldConfigSelectBean config=new UserFieldConfigSelectBean();
                config.setGroup(group);
                config.setFields(fieldBeans);
                list.add(config);
            }
            info.setData(list);
        } catch (Exception e) {
            info.setCode(Constant.MESSAGE_INT_FAIL);
            info.setDescription(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return info;
    }
}
